package cn.xh.org.controller;

import cn.xh.basic.util.AjaxResult;
import cn.xh.basic.util.PageList;
import cn.xh.org.domain.Department;
import cn.xh.org.query.DepartmentQuery;
import cn.xh.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理接口
 *   写RestFull风格的接口
 *   写接口的三要素
 *     1.访问路径是什么
 *     2.参数是什么
 *     3.返回值是什么
 *
 *     什么是RestFull风格的接口
 *     1.使用URL来定位资源
 *     2.使用http的动词来描述操作
 *
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

     //新增和修改  put
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() != null){
                departmentService.update(department);
            }else{
                departmentService.add(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage(e.getMessage());
        }
    }

    //删除  delete
    /**
     * PathVariable  从URL中获取占位符的值
     * @param id
     */
    @DeleteMapping("/{id}")   //  /dept/1
    public AjaxResult delete(@PathVariable("id") Long id){
        /*  很low
          try {
            departmentService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult result = new AjaxResult();
            result.setSuccess(false);
            result.setMessage("操作失败！"+e.getMessage());
            return result;
        }*/
        /* 稍微好一点点
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.failer(e.getMessage());
        }*/
        //链式编程思维
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage(e.getMessage());
        }


    }


    //查询一条  get
    @GetMapping("/{id}")
    public Department get(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    //查询所有  get
    @GetMapping("/list")
    public List<Department> getAll(){
        return departmentService.queryAll();
    }

    //分页+高级查询 post
    @PostMapping
    public PageList<Department> getPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     * 如果前端传的是，数组那么后端可以用  list 或者array接收都可以
     * @param ids 需要批量删除的id
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage(e.getMessage());
        }

    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }




}
